package com.example.demo.util;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class comparisonBean {
    public Map<String, Object> compareBean(Object newObject, Object oldObject) throws Exception {
        Map<String, Object> map = new HashMap<>();
        Field[] declaredFields = newObject.getClass().getDeclaredFields();
        for (Field field : declaredFields) {
            field.setAccessible(true);
            Object newObj = field.get(newObject);
            Object oldObj = field.get(oldObject);
            Annotation[] annotations = field.getAnnotations();
            if (annotations != null && annotations.length > 0) {
                Field[] declaredFields1 = newObj.getClass().getDeclaredFields();
                for (Field field1 : declaredFields1) {
                    field1.setAccessible(true);
                    Object newObj1 = field1.get(newObj);
                    Object oldObj1 = field1.get(oldObj);
                    Annotation[] annotations1 = field1.getAnnotations();
                    if (annotations1 != null && annotations1.length > 0) {
                        compareBean(newObj, oldObj);
                    } else {
                        if (newObj1 != null && newObj1.equals(oldObj1)) {
                            map.put(field1.getName(), newObj1);
                        }
                    }

                }

            } else {
                if (newObj != null && !newObj.equals(oldObj)) {
                    map.put(field.getName(), newObj);
                }
            }

        }
        return map;
    }
}
